package com.yuan.dao;

import com.yuan.pojo.User;
import com.yuan.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    public User findUser(String username, String password) throws SQLException {
        String sql="select * from user where username=? and password=?";
        User user = queryRunner.query(sql, new BeanHandler<>(User.class), username, password);
        return user;
    }

    public List<User> findUser() throws SQLException {
        String sql="select * from user" ;
        return queryRunner.query(sql, new BeanListHandler<>(User.class));
    }


    public void instertUser(User user) throws SQLException {
        String sql = "insert into user values (null,?,?,?,?,?,?,0)";
        queryRunner.update(sql,user.getUsername(),user.getPassword(),
                user.getAddress(),user.getNickname(),user.getGender(),user.getEmail());
    }


}
